package cn.xiyun.xiyuncc.web.controller;

import cn.xiyun.xiyuncc.domain.CourseChapter;
import cn.xiyun.xiyuncc.dto.CourseSaveDto;
import cn.xiyun.xiyuncc.dto.CourseSearchDto;
import cn.xiyun.xiyuncc.service.ICourseService;
import cn.xiyun.xiyuncc.domain.Course;
import cn.xiyun.xiyuncc.query.CourseQuery;
import cn.xiyun.xiyuncc.result.JSONResult;
import cn.xiyun.xiyuncc.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 秒杀订单信息
     * @return JSONResult
     */
    @RequestMapping(value = "/info/kill/{orderNo}", method = RequestMethod.GET)
    public JSONResult orderKillData(@PathVariable("orderNo") String orderNo) {
        return JSONResult.success(courseService.orderKillData(orderNo));
    }

    /**
     * 订单信息
     * @return JSONResult
     */
    @RequestMapping(value = "/orderData/{ids}", method = RequestMethod.GET)
    public JSONResult orderData(@PathVariable("ids") String ids) {
        return JSONResult.success(courseService.orderData(ids));
    }



    /**
     * 获取视频数据
     * @return JSONResult
     */
    @RequestMapping(value = "/getForUser/{id}", method = RequestMethod.GET)
    public JSONResult getForUser(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.getForUser(id));
    }



    /**
     * 课程详情页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/data/{id}", method = RequestMethod.GET)
    public JSONResult detailData(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.detailData(id));
    }



    /**
     * 课程上架
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/onLineCourse/{id}", method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long id) {

        courseService.onLineCourse(id);
        return JSONResult.success();
    }


    /**
     * 课程新增
     *
     * @param courseSaveDto
     * @return
     */
    @RequestMapping(value = "/courseAddition", method = RequestMethod.POST)
    public JSONResult courseAddition(@RequestBody CourseSaveDto courseSaveDto) {
        courseService.courseAddition(courseSaveDto);
        return JSONResult.success();
    }


    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course) {
        if (course.getId() != null) {
            courseService.updateById(course);
        } else {
            courseService.insert(course);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }



    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
